########################################################################
################ Converts DIPCON data into node lists ##################
########################################################################

#### For 1970-2010, creates: 
####### 1) a list of nodes ("stateIDs.csv") 

########################################################################
# Install packages
detach(package:statnet, unload=T)
install.packages("igraph")
install.packages("countrycode")
install.packages("plyr")
########################################################################

# Load libraries
library(igraph)
library(countrycode)
library(plyr)

# Set the working directory
setwd("~/Dropbox/ISQ_ms/5 Final/Replication")

# set savedir
SAVEDIR <- "~/Dropbox/ISQ_ms/5 Final/Replication"

# Read in data in adjacency matrix format (rows sending states, columns receiving states)
dipcon1970 <- read.csv("DIPCON1970_3.0.csv",row.names=1) # 134 states
dipcon1975 <- read.csv("DIPCON1975_3.0.csv",row.names=1) # 148 states
dipcon1980 <- read.csv("DIPCON1980_3.0.csv",row.names=1) # 157 states
dipcon1985 <- read.csv("DIPCON1985_3.0.csv",row.names=1) # 162 states
dipcon1990 <- read.csv("DIPCON1990_3.0.csv",row.names=1) # 164 states
dipcon1995 <- read.csv("DIPCON1995_3.0.csv",row.names=1) # 183 states
dipcon2000 <- read.csv("DIPCON2000_3.0.csv",row.names=1) # 187 states
dipcon2005 <- read.csv("DIPCON2005_3.0.csv",row.names=1) # 188 states
dipcon2010 <- read.csv("DIPCON2010_3.0.csv",row.names=1) # 190 states


##### Get list of nodes for each year

### For 1970
stateIDs1970 <- data.frame(row.names(dipcon1970))
colnames(stateIDs1970) <- "stateabb"
stateIDs1970$ccode <- countrycode(stateIDs1970$stateabb, "cowc", "cown")
table(is.na(stateIDs1970$ccode))
stateIDs1970$year <- 1970

### For 1975
stateIDs1975 <- data.frame(row.names(dipcon1975))
colnames(stateIDs1975) <- "stateabb"
stateIDs1975$ccode <- countrycode(stateIDs1975$stateabb, "cowc", "cown")
table(is.na(stateIDs1975$ccode))
stateIDs1975$year <- 1975

### For 1980
stateIDs1980 <- data.frame(row.names(dipcon1980))
colnames(stateIDs1980) <- "stateabb"
stateIDs1980$ccode <- countrycode(stateIDs1980$stateabb, "cowc", "cown")
table(is.na(stateIDs1980$ccode))
stateIDs1980$year <- 1980

### For 1985
stateIDs1985 <- data.frame(row.names(dipcon1985))
colnames(stateIDs1985) <- "stateabb"
stateIDs1985$ccode <- countrycode(stateIDs1985$stateabb, "cowc", "cown")
table(is.na(stateIDs1985$ccode))
stateIDs1985$year <- 1985

### For 1990
stateIDs1990 <- data.frame(row.names(dipcon1990))
colnames(stateIDs1990) <- "stateabb"
stateIDs1990$ccode <- countrycode(stateIDs1990$stateabb, "cowc", "cown")
table(is.na(stateIDs1990$ccode))
stateIDs1990$year <- 1990

### For 1995
stateIDs1995 <- data.frame(row.names(dipcon1995))
colnames(stateIDs1995) <- "stateabb"
stateIDs1995$ccode <- countrycode(stateIDs1995$stateabb, "cowc", "cown")
table(is.na(stateIDs1995$ccode))
stateIDs1995$year <- 1995

### For 2000
stateIDs2000 <- data.frame(row.names(dipcon2000))
colnames(stateIDs2000) <- "stateabb"
stateIDs2000$ccode <- countrycode(stateIDs2000$stateabb, "cowc", "cown")
table(is.na(stateIDs2000$ccode))
stateIDs2000$year <- 2000

### For 2005
stateIDs2005 <- data.frame(row.names(dipcon2005))
colnames(stateIDs2005) <- "stateabb"
stateIDs2005$ccode <- countrycode(stateIDs2005$stateabb, "cowc", "cown")
table(is.na(stateIDs2005$ccode))
stateIDs2005$year <- 2005

### For 2010
stateIDs2010 <- data.frame(row.names(dipcon2010))
colnames(stateIDs2010) <- "stateabb"
stateIDs2010$ccode <- countrycode(stateIDs2010$stateabb, "cowc", "cown")
table(is.na(stateIDs2010$ccode))
stateIDs2010$year <- 2010


##### Save state IDs for all years

stateIDs <- rbind(stateIDs1970, stateIDs1975, stateIDs1980, stateIDs1985, stateIDs1990, 
                    stateIDs1995, stateIDs2000, stateIDs2005, stateIDs2010)

write.csv(stateIDs, file=file.path(SAVEDIR, "stateIDs.csv"), row.names=F)

